package job

import (
	"fmt"
	"git.supremind.info/product/box/vehicle/global"
	"git.supremind.info/product/box/vehicle/internal/util"
	"git.supremind.info/product/box/vehicle/internal/vmr"
	"log"
	"path"
	"time"
)

func ReadVmrEvent() {
	go func() {
		for {
			var respData = vmr.EventAlarmRes{}
			err := util.Get(fmt.Sprintf("%s/v1/msg/group/filters/traffic_.+/event-detect-box", global.VmrSetting.Host), &respData)
			if err != nil {
				log.Println(err)
			} else if respData.Data != nil {
				if len(respData.Data) == 0 {
					log.Println("len(respData.Data) == 0")
				} else {
					for _, datum := range respData.Data {
						handleReadVmrEvent(datum)
					}
				}
			}
		}
	}()
}

func timeCost(start time.Time) {
	terminal := time.Since(start)
	fmt.Println(terminal)
}

func handleReadVmrEvent(eventAlarmVo vmr.EventAlarmVo) {
	defer timeCost(time.Now())

	var eventAlarm = eventAlarmVo.Body
	eventAlarm.Marking = "init"

	var taskRes = vmr.TaskRes{}
	var camera = vmr.EventCamera{}
	err := util.Get(fmt.Sprintf("%s/v1/task/%s", global.VmrSetting.Host, eventAlarm.TaskId), &taskRes)
	if err == nil && taskRes.Code == 0 {
		eventAlarm.TaskName = taskRes.Data.Name
		eventAlarm.TaskType = taskRes.Data.Type
		eventAlarm.CameraCode = taskRes.Data.StreamId
		camera.CameraCode = taskRes.Data.StreamId
	}

	camera.EvidenceUrl = eventAlarm.VideoUri
	eventAlarm.EvidenceUrl = eventAlarm.VideoUri
	eventAlarm.EventCameraList = append(eventAlarm.EventCameraList, camera)
	//log.Println(camera.EvidenceUrl)

	//snapshots := eventAlarm.Snapshot
	//for _, snapshot := range snapshots {
	//	if snapshot.FeatureUri != "" && len(snapshot.FeatureUri) > 0 {
	//		client.PutObject(getObjectName(snapshot.FeatureUri), snapshot.FeatureUri)
	//	}
	//	if snapshot.SnapshotUri != "" && len(snapshot.SnapshotUri) > 0 {
	//		client.PutObject(getObjectName(snapshot.SnapshotUri), snapshot.SnapshotUri)
	//	}
	//	if snapshot.SnapshotUriRaw != "" && len(snapshot.SnapshotUriRaw) > 0 {
	//		client.PutObject(getObjectName(snapshot.SnapshotUriRaw), snapshot.SnapshotUriRaw)
	//	}
	//	if snapshot.SnapshotUriThumb != "" && len(snapshot.SnapshotUriThumb) > 0 {
	//		client.PutObject(getObjectName(snapshot.SnapshotUriThumb), snapshot.SnapshotUriThumb)
	//	}
	//}

	//var url = strings.Replace(camera.EvidenceUrl, "localhost", "100.100.69.105", -1)
	//url = strings.Replace(url, "127.0.0.1", "100.100.69.105", -1)
}

func getObjectName(url string) string {
	var fileNameWithSuffix = path.Base(url)
	return "snapshot/" + time.Now().Format("2006/0102/15") + "/" + fileNameWithSuffix
}
